﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ECare.BUS;
using ECare.BUS.Data;

namespace ECare.Client.frms
{
    public partial class frmPlansReady : frmGridTemplate
    {
        public frmPlansReady()
        {
            InitializeComponent();

            this.Load += frmPlansReady_Load;

            this.grd.CellFormatting += grd_CellFormatting;
        }

        void grd_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            if (e.RowIndex >= 0 && e.ColumnIndex >= 0 && e.RowIndex < grd.Rows.Count)
            {
                if (grd.Columns[e.ColumnIndex].Name == "checkstatus")
                {
                    var currentstatus = grd.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString();
                    if (currentstatus == PlanStatus.pending.ToString())
                    {
                        this.grd.Rows[e.RowIndex].DefaultCellStyle.ForeColor = System.Drawing.Color.Green;
                    }
                    else if (currentstatus == PlanStatus.suspend.ToString())
                    {
                        this.grd.Rows[e.RowIndex].DefaultCellStyle.ForeColor = System.Drawing.Color.Red;
                    }
                    else
                    {

                    }

                }
            }
        }



        public BindingList<PlanInfo> Source { get; set; }
        void frmPlansReady_Load(object sender, EventArgs e)
        {

            formula_id.DataSource = FormulaHelper.GetFormulaInfos();
            formula_id.DisplayMember = "FormulaName";
            formula_id.ValueMember = "FormulaId";




            if (!CheckPermission(7))
                this.btnAdd.Enabled = false;

            if (!CheckPermission(6))
                this.btnDelete.Enabled = false;

            if (!CheckPermission(8))
                this.btnExecute.Enabled = false;

           // this.LoadData("1=1");

        }

        //private async void LoadData(string where)
        //{
        //    this.Source = new BindingList<PlanInfo>(PlanHelper.GetPlans(where,true));
        //    this.grd.DataSource = this.Source;
        //}


        private void btnAdd_Click(object sender, EventArgs e)
        {
            if (this.grd.SelectedRows.Count == -1)
                return;

            var selectedplan = this.grd.SelectedRows[0].DataBoundItem as PlanInfo;

            if (selectedplan.checkstatus != PlanStatus.qualified)
                return;

            bool startresult = PlanHelper.StartToExecutePlan(selectedplan);
            if (startresult == true)
            {
                //等待进行中....
                selectedplan.checkstatus = PlanStatus.pending;
            }

        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            if (this.grd.SelectedRows.Count == 0)
                return;

            var selectedplan = this.grd.SelectedRows[0].DataBoundItem as PlanInfo;

            var frm = new frmPlanDetails();
            frm.entity = selectedplan;
            frm.ShowDialog();

        }

        private void btnExecute_Click(object sender, EventArgs e)
        {
            if (this.grd.SelectedRows.Count == 0)
                return;
            var selectedplan = this.grd.SelectedRows[0].DataBoundItem as PlanInfo;
            var frmwizard = new frmWizard();
            frmwizard.currentPlan = selectedplan;
            frmwizard.ShowDialog();
        }

        private void eButton1_Click(object sender, EventArgs e)
        {
            var frm = new frmPlanQuery();
            frm.SearchCompleted += frm_SearchCompleted;frm.ShowDialog();
        }

        void frm_SearchCompleted(string where)
        {
            //throw new NotImplementedException();
        //    this.LoadData(where);
        }
    }
}
